package edu.cmu.vlis.wassup.sender;




import org.mybeans.dao.DAOException;
import org.mybeans.factory.RollbackException;

import edu.cmu.vlis.wassup.databean.Event;
import edu.cmu.vlis.wassup.db.EventDAO;
import edu.cmu.vlis.wassup.db.EventTagDAO;


public class DBHandler {
    
    EventDAO am = null;  
    EventTagDAO atm = null;
    private static DBHandler handler = null;
    String topic;
    
    private DBHandler(){   	
        try {
			am = new EventDAO();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}     
    }
    
    public static DBHandler getInstance(){
        if( null == handler)
            handler = new DBHandler();
       return handler;
    }
    
    public void setTopic(String topic) {
    	this.topic = topic;
    }
    
    public String commitToDB( Event a){
    	String a1=null;
        try {
            a1 = am.insert(a,topic);
            System.out.println("Committed to DB: "+a.getHashId());
            return a1;
        } catch (RollbackException e) {
            System.err.println("Skipping msg.. Already there in DB: "+a.getHashId() );
            System.err.println(e.toString());
        }
        return null;
    }
    

}
